<script setup lang="ts">
import { ref, reactive, watch } from 'vue';
import { ElMessage } from 'element-plus';

interface DriverForm {
  id?: string;
  driverName: string;
  company: string;
  phone: string;
  idCard: string;
  licenseType: string;
  licenseExpiry: string;
  qualificationExpiry: string;
  homeAddress: string;
  emergencyContact: string;
  emergencyPhone: string;
  status: number;
  remark: string;
}

const props = defineProps<{
  visible: boolean;
  driverData?: DriverForm | null;
  isEdit?: boolean;
}>();

const emit = defineEmits<{
  'update:visible': [value: boolean];
  'confirm': [data: DriverForm];
}>();

// 表单数据
const formData = reactive<DriverForm>({
  driverName: '',
  company: '',
  phone: '',
  idCard: '',
  licenseType: 'C1',
  licenseExpiry: '',
  qualificationExpiry: '',
  homeAddress: '',
  emergencyContact: '',
  emergencyPhone: '',
  status: 1,
  remark: ''
});

// 表单引用
const formRef = ref();

// 驾照类型选项
const licenseTypeOptions = [
  { value: 'A1', label: 'A1' },
  { value: 'A2', label: 'A2' },
  { value: 'B1', label: 'B1' },
  { value: 'B2', label: 'B2' },
  { value: 'C1', label: 'C1' },
  { value: 'C2', label: 'C2' }
];

// 状态选项
const statusOptions = [
  { value: 1, label: '在职' },
  { value: 0, label: '离职' },
  { value: 2, label: '停职' }
];

// 表单验证规则
const rules = {
  driverName: [
    { required: true, message: '请输入司机姓名', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' }
  ],
  idCard: [
    { required: true, message: '请输入身份证号', trigger: 'blur' }
  ],
  licenseType: [
    { required: true, message: '请选择驾照类型', trigger: 'change' }
  ],
  licenseExpiry: [
    { required: true, message: '请选择驾照到期时间', trigger: 'change' }
  ],
  qualificationExpiry: [
    { required: true, message: '请选择从业资格证到期时间', trigger: 'change' }
  ],
  homeAddress: [
    { required: true, message: '请输入家庭住址', trigger: 'blur' }
  ],
  emergencyContact: [
    { required: true, message: '请输入紧急联系人', trigger: 'blur' }
  ],
  emergencyPhone: [
    { required: true, message: '请输入紧急联系人电话', trigger: 'blur' }
  ]
};

// 监听弹窗显示状态
watch(() => props.visible, (newVal) => {
  if (newVal) {
    // 重置表单
    resetForm();
    // 如果是编辑模式，填充数据
    if (props.isEdit && props.driverData) {
      Object.assign(formData, props.driverData);
    }
  }
});

// 重置表单
const resetForm = () => {
  Object.assign(formData, {
    driverName: '',
    company: '',
    phone: '',
    idCard: '',
    licenseType: 'C1',
    licenseExpiry: '',
    qualificationExpiry: '',
    homeAddress: '',
    emergencyContact: '',
    emergencyPhone: '',
    status: 1,
    remark: ''
  });
  formRef.value?.clearValidate();
};

// 关闭弹窗
const handleClose = () => {
  emit('update:visible', false);
};

// 确认提交
const handleConfirm = async () => {
  try {
    await formRef.value?.validate();
    emit('confirm', { ...formData });
    handleClose();
    ElMessage.success(props.isEdit ? '编辑成功' : '新增成功');
  } catch (error) {
    console.error('表单验证失败:', error);
  }
};
</script>

<template>
  <el-dialog
    :model-value="visible"
    :title="isEdit ? '编辑司机' : '新增司机'"
    width="600px"
    @update:model-value="handleClose"
    @close="handleClose"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item label="司机姓名" prop="driverName">
        <el-input v-model="formData.driverName" placeholder="请输入司机姓名" />
      </el-form-item>
      
      <el-form-item label="物流公司" prop="company">
        <el-input v-model="formData.company" placeholder="请输入物流公司" />
      </el-form-item>
      
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="formData.phone" placeholder="请输入手机号" />
      </el-form-item>
      
      <el-form-item label="身份证号" prop="idCard">
        <el-input v-model="formData.idCard" placeholder="请输入身份证号" />
      </el-form-item>
      
      <el-form-item label="驾照类型" prop="licenseType">
        <el-select v-model="formData.licenseType" placeholder="请选择驾照类型" style="width: 100%">
          <el-option
            v-for="item in licenseTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      
      <el-form-item label="驾照到期时间" prop="licenseExpiry">
        <el-date-picker
          v-model="formData.licenseExpiry"
          type="date"
          placeholder="请选择驾照到期时间"
          style="width: 100%"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      
      <el-form-item label="从业资格证到期" prop="qualificationExpiry">
        <el-date-picker
          v-model="formData.qualificationExpiry"
          type="date"
          placeholder="请选择从业资格证到期时间"
          style="width: 100%"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      
      <el-form-item label="家庭住址" prop="homeAddress">
        <el-input
          v-model="formData.homeAddress"
          type="textarea"
          :rows="2"
          placeholder="请输入家庭住址"
        />
      </el-form-item>
      
      <el-form-item label="紧急联系人" prop="emergencyContact">
        <el-input v-model="formData.emergencyContact" placeholder="请输入紧急联系人" />
      </el-form-item>
      
      <el-form-item label="紧急联系电话" prop="emergencyPhone">
        <el-input v-model="formData.emergencyPhone" placeholder="请输入紧急联系电话" />
      </el-form-item>
      
      <el-form-item label="状态" prop="status">
        <el-select v-model="formData.status" placeholder="请选择状态" style="width: 100%">
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="formData.remark"
          type="textarea"
          :rows="2"
          placeholder="请输入备注"
        />
      </el-form-item>
    </el-form>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">
          {{ isEdit ? '保存' : '确定' }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>